﻿using System;
using nRoute.Components;
using nRoute.Components.Composition;
using nRoute.Navigation.Mapping;
using nRoute.ViewModels;
using Officer2010.Content.Views;
using Officer2010.Infrastructure.Components;
using Officer2010.Infrastructure.ViewServices;

namespace Officer2010.Content.ViewModels
{
    [DefineNavigationContent("Dialogs/ShareDialog", typeof(ShareDialog))]
    [MapViewModel(typeof(ShareDialog))]
    public class ShareDialogViewModel 
        : NavigationViewModelBase
    {
        private const string NAVIGATION_TITLE = "Share Document";
        private const string USERNAME_KEY = "UserNameKey";
        private const string INITIALS_KEY = "InitialsKey";

        private string _userName;
        private string _initials;
        private readonly Lazy<IMessageViewService> _messageViewService;

        [ResolveConstructor]
        public ShareDialogViewModel(Lazy<IMessageViewService> messageViewService)
        {
            base.Title = NAVIGATION_TITLE;
            _messageViewService = messageViewService;
            SetupCommands();
            LoadState();
        }

#region Properties

        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
                NotifyPropertyChanged(() => UserName);
            }
        }

        public string Initials
        {
            get
            {
                return _initials;
            }
            set
            {
                _initials = value;
                NotifyPropertyChanged(() => Initials);
            }
        }

        public ActionCommand SaveCommand { get; private set; }
        
#endregion

#region Helpers

        private void SetupCommands()
        {
            SaveCommand = new ActionCommand(() =>
            {
                PersistState();
                _messageViewService.Value.ShowMessage("Your Identifiers Saved");
            });
        }

        private void PersistState()
        {
            IsolatedStore.SaveText(this.UserName, USERNAME_KEY);
            IsolatedStore.SaveText(this.Initials, INITIALS_KEY);
        }

        private void LoadState()
        {
            this.UserName = IsolatedStore.LoadText(USERNAME_KEY);
            this.Initials = IsolatedStore.LoadText(INITIALS_KEY);
        }

#endregion

    }
}
